﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Dazaza.UI.Mvc.Models;
using Dazaza.Domain.Model;
using Dazaza.Domain.Model.SearchInfo;
using Dazaza.Repositories;

namespace Dazaza.UI.Mvc.Controllers
{
    public class HomeController : BaseController
    {
        [OutputCache(Duration = 1600)]//不指定 VaryByParam，默认会根据所有param
        public ActionResult Index(Enums.EnumTab? Tab, int? pageindex, string categoryclass, string category)
        {
            Tab = Tab ?? Enums.EnumTab.Hot;
            string queryCategory = category ?? string.Empty; //Request.QueryString["category"] ?? string.Empty;//dbo.[category].name_en
            string queryCategoryClass = (categoryclass ?? string.Empty); //queryCategory == string.Empty ? string.Empty : (queryCategory.IndexOf("_") > 0 ? queryCategory.Substring(0, queryCategory.IndexOf('_')) : string.Empty);
            string queryListType = Request.QueryString["listtype"] ?? string.Empty;
            string queryPageIndex = Request.QueryString["pageindex"] ?? string.Empty;

            HomeIndexViewModel model = new HomeIndexViewModel(masterViewModel);

            //如果category不包含_，则说明category是categorycalss
            if (queryCategory != string.Empty && queryCategory.Contains("_") == false)
            {
                queryCategoryClass = category;
                queryCategory = string.Empty;
            }


            // hot or upcoming
            EnumInfoListType listType = EnumInfoListType.Hot;
            if (Tab == Enums.EnumTab.Upcoming)
            {
                listType = EnumInfoListType.Upcoming;
                model.MasterViewModel.HeaderInfo.PageTitle = "新发现 —— " + Common.CommonData.SiteSlogan;
            }
            if (Tab == Enums.EnumTab.Hot)
            {
                listType = EnumInfoListType.Hot;
                model.MasterViewModel.HeaderInfo.PageTitle = Common.CommonData.SiteSlogan;
            }

            // searchInfo
            InfoSearchInfo searchInfo = new InfoSearchInfo()
            {
                PageIndex = pageindex ?? 1,
                PageSize = this.masterViewModel.PagerInfo.PageSize,
                ListType = listType,
                Category = queryCategory,
                CategoryClass = queryCategoryClass
            };




            model.MasterViewModel.HeaderInfo.PageTitle = Dazaza.Common.CommonData.SiteSlogan;
            model.Tab = Tab ?? Enums.EnumTab.Hot;
            model.InfoModelList = InfoRepository.Search(searchInfo);

            //pager
            model.Pager = new PagerInfo()
            {
                PageIndex = searchInfo.PageIndex,
                RecordCount = searchInfo.RecordCount
            };
            if (Tab == Enums.EnumTab.Upcoming)
            {
                model.Pager.LinkFormat = "/upcoming_p{0}";
                model.Pager.PrePageLinkParameters = new string[] { (model.Pager.PageIndex - 1).ToString() };
                model.Pager.NextPageLinkParameters = new string[] { (model.Pager.PageIndex + 1).ToString() };
                if (queryCategory != string.Empty || queryCategoryClass != string.Empty)
                {
                    model.Pager.LinkFormat = "/upcoming_{1}_p{0}";
                    model.Pager.PrePageLinkParameters = new string[] { (model.Pager.PageIndex - 1).ToString(), queryCategory == string.Empty ? queryCategoryClass : queryCategory };
                    model.Pager.NextPageLinkParameters = new string[] { (model.Pager.PageIndex + 1).ToString(), queryCategory == string.Empty ? queryCategoryClass : queryCategory };
                }
            }
            if (Tab == Enums.EnumTab.Hot)
            {
                model.Pager.LinkFormat = "/hot_p{0}";
                model.Pager.PrePageLinkParameters = new string[] { (model.Pager.PageIndex - 1).ToString() };
                model.Pager.NextPageLinkParameters = new string[] { (model.Pager.PageIndex + 1).ToString() };
                if (queryCategory != string.Empty || queryCategoryClass != string.Empty)
                {
                    model.Pager.LinkFormat = "/{1}_p{0}";
                    model.Pager.PrePageLinkParameters = new string[] { (model.Pager.PageIndex - 1).ToString(), queryCategory == string.Empty ? queryCategoryClass : queryCategory };
                    model.Pager.NextPageLinkParameters = new string[] { (model.Pager.PageIndex + 1).ToString(), queryCategory == string.Empty ? queryCategoryClass : queryCategory };
                }
            }

            return View(model);
        }

    }
}
